/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.cbr.similarity;

import de.dfki.mycbr.XMLConstants;
import de.dfki.mycbr.model.similaritymeasures.AbstractSMFunction;
import de.dfki.mycbr.model.similaritymeasures.smftypes.SMF_Number_Advanced;
import de.dfki.mycbr.model.similaritymeasures.smftypes.SMF_Symbol_Table;
import de.dfki.mycbr.model.vocabulary.ModelInstance;
import java.util.ArrayList;
import java.util.Iterator;
import jcolibri.exception.NoApplicableSimilarityFunctionException;
import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.filter.ElementFilter;
import org.jdom.input.SAXBuilder;


/**
 *
 * @author Geir Ole
 */
public class TrendSimilarity implements LocalSimilarityFunction{
 
    public AbstractSMFunction smf = null;

    /**
     * Constructor.
     * @param configFilePath is the path to the XML file generated with MyCBR
     */
    public TrendSimilarity(String configFilePath)
    {
        try {
            SAXBuilder saxBuilder = new SAXBuilder();
            Document docSMF = saxBuilder.build(jcolibri.util.FileIO.findFile(configFilePath));
            for (Iterator fctIt = docSMF.getContent(new ElementFilter(XMLConstants.XML_TAG_SMFUNCTION)).iterator(); fctIt.hasNext();)
            {
                // get next similarity function
                Element smfElement = (Element) fctIt.next();

                // according slot
                Attribute att = smfElement.getAttribute(XMLConstants.XML_ATT_MODEL_INSTNAME);
                String instname = att.getValue();

                Attribute attype = smfElement.getAttribute(XMLConstants.XML_ATT_TYPE);
                String insttype = attype.getValue();

                ArrayList<Object> av = new ArrayList<Object>();
                ModelInstance instance = new PuppetModelInstance(instname, insttype, av);

                smf = new SMF_Symbol_Table(instance,smfElement);
            }
        } catch (Exception e) {
            org.apache.commons.logging.LogFactory.getLog(this.getClass()).error("Error initializing Trend similarity function");
        }
    }
    public double compute(Object caseObject, Object queryObject) throws NoApplicableSimilarityFunctionException {
        if(!isApplicable(caseObject, queryObject))
            return 0.0;
        
        double res = 0;
        try {
                res = smf.getSimilarityBetween(caseObject, queryObject, null);
        } catch (Exception e) {
                org.apache.commons.logging.LogFactory.getLog(this.getClass()).error("Error executing MyCBR similarity function");
        }
        return res;
    }

    public boolean isApplicable(Object caseObject, Object queryObject) {
        return (caseObject instanceof String)&&(queryObject instanceof String);
    }
    public static void main(String[] args) {
        TrendSimilarity ts = new TrendSimilarity("my/cbr/similarity/resources/trend.xml");
        try {
            System.out.println(ts.compute("IncreasingWeakly", "Unknown"));
        } catch (NoApplicableSimilarityFunctionException noApplicableSimilarityFunctionException) {
        }
    }
}
